package com.yyaccp.appinfo.service;

import com.yyaccp.appinfo.domain.DevUser;
import com.yyaccp.appinfo.exception.LoginException;
import com.yyaccp.appinfo.mapper.DevUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("devUserService")
public class DevUserService {

    @Autowired
    private DevUserMapper devUserMapper;
    @Autowired
    private SmsService smsService;


    public DevUser devLogin(DevUser User) throws LoginException {
        DevUser devUser = devUserMapper.selectByDevCode(User.getDevCode());
        if(devUser == null){
            throw new LoginException("账号输入错误!");
        }
        if(!devUser.getDevPassword().equals(User.getDevPassword())){
            throw new LoginException("密码输入错误!");
        }
        return devUser;
    }

    public DevUser findByDevPhone(String devPhone){
        return devUserMapper.selectByDevPhone(devPhone);
    }

    public DevUser login(String devPhone,String code) throws  LoginException{
        DevUser user = devUserMapper.selectByDevPhone(devPhone);
        if(user == null){
            throw  new LoginException("手机号码不存在");
        }
        String dbCode = smsService.getCode(devPhone);
        if(dbCode == null){
            throw new LoginException("验证码已经失效");
        }
        if(!code.equals(dbCode)){
            throw new LoginException("验证码错误");
        }
        return user;
    }
}
